package {
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	
	import mx.graphics.codec.JPEGEncoder;

	public class FileWrite extends EventDispatcher {
		private static var _instance:FileWrite;

		private var _urlStr:String;
		private var _file:File;
		private var _xml:XML;
		private var _fileStream:FileStream;
		private var _outPutFile:File;
		private var _outPutXML:XML;
		private var _outBitMapData:BitmapData;
		private var _outPutBgURL:String;

		public function FileWrite(single:Single) {
			_file = new File("/未命名.ls");
		}

		public static function getInstance():FileWrite {
			if (_instance == null) {
				_instance = new FileWrite(new Single());
			}
			return _instance;
		}

		public function save(data:XML):void {
			_xml = data;

			if (_file.exists) {
				_file.deleteFile();
				onSelectedHandle(null);
			} else {
				_file.browseForSave("另存为...");
				_file.addEventListener(Event.SELECT, onSelectedHandle);
			}

		}

		private function onSelectedHandle(event:Event = null):void {
			if (event) {
				var targetFile:File = event.target as File;

				targetFile.removeEventListener(Event.SELECT, onSelectedHandle);
			}

			var original:File = new File(_xml.bgPath);

			var path:String = _file.parent.nativePath + "/" + original.name;

			var newFile:File = new File(path);
			original.copyTo(newFile, true);

			//把文件复制过来后可以保证因为删除以前的背景导致打开文件出错!
			_xml.bgPath = newFile.nativePath;
			_fileStream = new FileStream();
			_fileStream.openAsync(_file, FileMode.WRITE);
			_fileStream.writeUTFBytes(_xml);
			_fileStream.close();
		}

		public function saveAs(data:XML):void {
			_xml = data;
			_file.browseForSave("另存为...");
			_file.addEventListener(Event.SELECT, onSelectedHandle);
		}

		public function destroy():void {
			_file = null;
			_fileStream = null;
			_xml = null;
			_urlStr = null;
			_outPutFile = null;
			_outBitMapData = null;
		}

		/**
		 *导出数据到文件夹
		 * @param data
		 * @param bitMapData
		 *
		 */
		public function outPutFile(data:XML, bitMapData:BitmapData, bgswfURL:String):void {
			_outPutXML = data;
			_outBitMapData = bitMapData;
			_outPutBgURL = bgswfURL;

			_outPutFile = File.documentsDirectory;
			_outPutFile.addEventListener(Event.SELECT, onDirectorySelectHandle);
			_outPutFile.browseForDirectory("选择目录");

		}

		private function onDirectorySelectHandle(event:Event):void {
			_outPutFile.removeEventListener(Event.SELECT, onDirectorySelectHandle);
			//xml数据
			var xmlFile:File = new File(_outPutFile.nativePath + "/config.xml");
			var xmlFileStream:FileStream = new FileStream();
			xmlFileStream.openAsync(xmlFile, FileMode.WRITE);
			xmlFileStream.writeUTFBytes(_outPutXML);
			xmlFileStream.close();

			//背景swf
			var original:File = new File(_outPutBgURL);
			var bgswfFile:File = new File(_outPutFile.nativePath + "/map.swf");
			original.copyTo(bgswfFile, true);

			//缩略图
			var jpgEncode:JPEGEncoder = new JPEGEncoder(50);
			var encodeByteArray:ByteArray = jpgEncode.encode(_outBitMapData);

			var jpgFile:File = new File(_outPutFile.nativePath + "/miniJPG.jpg");
			var jpgFileStream:FileStream = new FileStream();
			jpgFileStream.openAsync(jpgFile, FileMode.WRITE);
			jpgFileStream.writeBytes(encodeByteArray, 0, encodeByteArray.bytesAvailable);
			jpgFileStream.close();
		}
	}
}

class Single {

}
